from itsdangerous.jws import TimedJSONWebSignatureSerializer as TJWSSerializer
from django.conf import settings


def encode_openid(openid):
    serializer = TJWSSerializer(secret_key=settings.SECRET_KEY, expires_in=300)
    token = serializer.dumps({'openid': openid})
    return token.decode()

def decode_openid(data):
    serializer = TJWSSerializer(secret_key=settings.SECRET_KEY, expires_in=300)
    try:
        dict_data = serializer.loads(data)
    except Exception:
        return None
    return dict_data.get('openid')